# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=gthumb
pkgver=3.12.2
pkgrel=0
pkgdesc="Image viewer and browser from GNOME"
url="https://gitlab.gnome.org/GNOME/gthumb"
arch="all !riscv64 !s390x"  # blocked by colord, librsvg
license="GPL-2.0-or-later"
depends="hicolor-icon-theme"
makedepends="
	bison
	clutter-gtk-dev
	colord-dev
	exiv2-dev
	flex
	gsettings-desktop-schemas-dev
	gst-plugins-base-dev
	gstreamer-dev
	gtk+3.0-dev
	itstool
	jpeg-dev
	json-glib-dev
	lcms2-dev
	libchamplain-dev
	libheif-dev
	libjxl-dev
	libpng-dev
	libraw-dev
	librsvg-dev
	libsecret-dev
	libsoup-dev
	meson
	webkit2gtk-dev
	"
sonameprefix="$pkgname:"
# Ignore libcairo_io.so in automatically traced dependencies to avoid conflict
# between gthumb and gthumb-extra-formats (both provides this extension library).
somask="libcairo_io.so"
subpackages="
	$pkgname-dev
	$pkgname-doc
	$pkgname-lang
	$pkgname-extra-formats:extra_formats
	$pkgname-full
	"
source="https://gitlab.gnome.org/GNOME/gthumb/-/archive/$pkgver/gthumb-$pkgver.tar.gz"

# List of extensions splitted into subpackages.
_extensions="
	23hq:_23hq
	flickr
	gstreamer
	map-view:map_view
	oauth
	raw-files:raw_files
	webalbums
	"
_depends_full="
	$pkgname=$pkgver-r$pkgrel
	$pkgname-extra-formats
	"
for _i in $_extensions; do
	subpackages="$subpackages $pkgname-$_i"
	_depends_full="$_depends_full $pkgname-${_i%:*}"
done

_extsdir='usr/lib/gthumb/extensions'

build() {
	msg 'Building full variant'
	_build output true

	msg 'Building smaller variant'
	_build output-small false
}

_build() {
	local outdir=$1
	local toggle=$2

	abuild-meson \
		-Dwarn-deprecated=false \
		-Dexiv2=true \
		-Dclutter=false \
		-Dgstreamer=$toggle \
		-Dlibchamplain=true \
		-Dlcms2=true \
		-Dcolord=true \
		-Dlibtiff=false \
		-Dlibwebp=false \
		-Dlibjxl=$toggle \
		-Dlibheif=$toggle \
		-Dlibraw=true \
		-Dlibrsvg=$toggle \
		-Dlibsecret=true \
		-Dwebservices=true \
		-Dlibbrasero=false \
		. "$outdir"
	meson compile ${JOBS:+-j ${JOBS}} -C "$outdir"
}

check() {
	meson test --no-rebuild -v -C output
}

package() {
	DESTDIR="$pkgdir" meson install --no-rebuild -C output

	_rename "$pkgdir"/usr/bin/gthumb gthumb.gst
	_rename "$pkgdir"/usr/lib/gthumb/extensions/libslideshow.so libslideshow.so.gst
	_rename "$pkgdir"/usr/lib/gthumb/extensions/libcairo_io.so libcairo_io.so.extra

	DESTDIR="$pkgdir" meson install --no-rebuild -C output-small

	cd "$pkgdir"

	# Keep only "C" (English) variant of help.
	mv usr/share/help/C .
	rm -rf usr/share/help/*
	mv C usr/share/help/
}

_23hq() {
	pkgdesc="gThumb extension for uploading images to 23hq.com"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove $_extsdir/23hq.extension
	amove $_extsdir/lib23hq.so
}

extra_formats() {
	pkgdesc="gThumb support for AVIF, HEIF, JPEG XL, and SVG image formats"
	depends="$pkgname=$pkgver-r$pkgrel"
	replaces="$pkgname"  # libcairo_io.so

	amove $_extsdir/libcairo_io.so.extra
	_rename "$subpkgdir"/$_extsdir/libcairo_io.so.extra libcairo_io.so
}

flickr() {
	pkgdesc="gThumb extension for uploading images to Flickr"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove $_extsdir/flicker*.extension
	amove $_extsdir/libflicker*.so
}

gstreamer() {
	pkgdesc="gThumb extension for playing audio and video files"
	depends="$pkgname=$pkgver-r$pkgrel"
	replaces="$pkgname"  # gthumb and libslideshow.so

	amove $_extsdir/gstreamer_*.extension
	amove $_extsdir/libgstreamer_*.so

	amove usr/bin/gthumb.gst
	_rename "$subpkgdir"/usr/bin/gthumb.gst gthumb

	amove $_extsdir/libslideshow.so.gst
	_rename "$subpkgdir"/$_extsdir/libslideshow.so.gst libslideshow.so
}

map_view() {
	pkgdesc="gThumb extension for viewing the photo position on the map"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove $_extsdir/map_view.extension
	amove $_extsdir/libmap_view.so
}

oauth() {
	pkgdesc="OAuth support for gThumb"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove $_extsdir/oauth.extension
	amove $_extsdir/liboauth.so
}

raw_files() {
	pkgdesc="gThumb extension for RAW format support"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove $_extsdir/raw_files.extension
	amove $_extsdir/libraw_files.so
}

webalbums() {
	pkgdesc="gThumb extension for creating static webalbums"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove $_extsdir/webalbums.extension
	amove $_extsdir/libwebalbums.so
	amove usr/share/gthumb/albumthemes
}

full() {
	pkgdesc="$pkgdesc (with all built-in extensions)"
	depends="$_depends_full"

	mkdir -p "$subpkgdir"
}

_rename() {
	mv "$1" "${1%/*}/$2"
}

sha512sums="
3066c5dd5fdd10fa5f869195adfd9fd3b5ee26cb7902cb87d324a90af535108558a223989ed736a941b8f6ce56e29c27a675da10fa1fb4f76919ced6a3c78b61  gthumb-3.12.2.tar.gz
"
